package com.lupan.passwordbox.common.validator;

import com.lupan.passwordbox.domain.Person;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 * 说明：
 *
 * @author lupan
 * @version 2017/8/15 15:29
 */
public class PersonValidator implements Validator{

    @Override
    public boolean supports(Class<?> clazz) {
        return Person.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        ValidationUtils.rejectIfEmpty(errors,"name","name.empty");
        Person person = (Person) target;
        if(person.getAge()>120){
             errors.rejectValue("age","age is too old");
        }else if(person.getAge()<0){
            errors.rejectValue("age","age is negative");
        }
    }
}
